package com.workManager.controller;

import com.github.pagehelper.PageInfo;
import com.wordnik.swagger.annotations.ApiOperation;
import com.workManager.base.BaseController;
import com.workManager.config.RespCode;
import com.workManager.pojo.TdFunction;
import com.workManager.service.FunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;


import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by Thinkpad on 2017/3/3 0003.
 */
@Controller
@RequestMapping("/function")
public class FunctionController extends BaseController {

    @Autowired
    private FunctionService functionService ;

    @ApiOperation(value="功能添加",notes = "功能添加")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public  Object add(HttpServletRequest request, @RequestBody TdFunction function){
        ModelMap modelMap = new ModelMap();
        function.setIsValid(true);
        function.setCreateDate(new Date());


        /*Map<String,Object> map = new HashMap<String, Object>();
        map.put("functionName",function.getFunctionName());
        PageInfo<TdFunction> functionPageInfo = functionService.query(map);
        //验证加入的权限有没有重复
        if(functionPageInfo.getTotal()>0){
            List<TdFunction> functionList = functionPageInfo.getList();
            for(TdFunction tdFunction:functionList){
                if(tdFunction.getFunctionName().equals(function.getFunctionName())){
                    return  setModelMap(modelMap, RespCode.INTERNAL_SERVER_ERROR,"权限名："+function.getFunctionName()+"已存在！");
                }
            }

        }*/

        try{
            functionService.add(function);
        }catch (DuplicateKeyException e){
            return setModelMap(modelMap,RespCode.INTERNAL_SERVER_ERROR,"权限名:"+function.getFunctionName()+" 已经存在!");
        }


        return  setSuccessModelMap(modelMap,"新增成功");
    }

    @ApiOperation(value="功能删除",notes = "功能删除")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public  Object delete(HttpServletRequest request, @RequestParam String functionId){
        ModelMap modelMap = new ModelMap();
        functionService.delete(functionId);
        return  setSuccessModelMap(modelMap);
    }

    @ApiOperation(value="功能批量删除",notes = "功能批量删除")
    @RequestMapping(value = "/deleteByList",method = RequestMethod.POST)
    public  Object deleteByList(@RequestParam List<String> functionIds){
        ModelMap modelMap = new ModelMap();
        functionService.deleteByList(functionIds);
        return  setSuccessModelMap(modelMap);
    }

    @ApiOperation(value="功能更新",notes = "功能更新")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public  Object update(HttpServletRequest request, @RequestBody TdFunction function){
        ModelMap modelMap = new ModelMap();
        function.setIsValid(true);

        /*Map<String,Object> map = new HashMap<String, Object>();
        map.put("functionId",function.getFunctionId());
        PageInfo<TdFunction> functionPageInfo = functionService.query(map);
        if(function.getFunctionName()!=null&&!function.getFunctionName().equals(functionPageInfo.getList().get(0).getFunctionName())){

            Map<String,Object> map1 = new HashMap<String, Object>();
            map1.put("functionName",function.getFunctionName());
            PageInfo<TdFunction> pageInfo =  functionService.query(map1);
            if(pageInfo.getTotal()>1){
                return  setModelMap(modelMap,RespCode.INTERNAL_SERVER_ERROR,"权限名："+function.getFunctionName()+" 已存在！");
            }
        }*/

        try {
            functionService.update(function);
        }catch (DuplicateKeyException e){
            return setModelMap(modelMap,RespCode.INTERNAL_SERVER_ERROR,"权限名:"+function.getFunctionName()+" 已存在！");
        }

        return  setSuccessModelMap(modelMap,"修改成功");
    }

    @ApiOperation(value="功能查询",notes = "功能查询")
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public  Object query(HttpServletRequest request,@RequestParam(value = "pageNumber",required = false) String pageNumber,
                         @RequestParam(value="pageSize", required = false) String pageSize, @RequestParam(value = "functionName",required = false) String functionName,
                         @RequestParam(value = "groupCode",required = false) String groupCode,@RequestParam(value="functionCode",required = false) String functionCode){
        ModelMap modelMap = new ModelMap();
        Integer pagenum = pageNumber==null?1:Integer.parseInt(pageNumber);
        Integer pagesize = pageSize==null?10:Integer.parseInt(pageSize);
        Map<String,Object> dataMap = new HashMap<String, Object>();
        Map<String,Object> map = new HashMap();

        map.put("pagenum",pagenum);
        map.put("pagesize",pagesize);
        map.put("functionName",functionName);
        map.put("functionCode",functionCode);
        map.put("groupCode",groupCode);
        PageInfo<TdFunction> functionPageInfo = functionService.query(map);
        dataMap.put("total",functionPageInfo.getTotal());
        dataMap.put("rows",functionPageInfo.getList());
        return  setSuccessModelMap(modelMap,dataMap);
    }

    @ApiOperation(value="功能查询",notes = "按组返回")
    @RequestMapping(value = "/queryAsGroup",method = RequestMethod.POST)
    public Object queryAsGroup(@RequestParam(value = "pageNumber",required = false) String pageNumber,
                               @RequestParam(value="pageSize", required = false) String pageSize){
        ModelMap modelMap = new ModelMap();
        Integer pagenum = pageNumber==null?1:Integer.parseInt(pageNumber);
        Integer pagesize = pageSize==null?10:Integer.parseInt(pageSize);
        List<Map<String,Object>> listMap = new ArrayList<Map<String, Object>>();

        Map parammap = new HashMap();
        parammap.put("pagenum",pagenum);
        parammap.put("pagesize",pagesize);
        parammap.put("pagesize",0);
        parammap.put("functionName",null);
        parammap.put("functionCode",null);
        parammap.put("groupCode",null);

        PageInfo<TdFunction> functionPageInfo = functionService.query(parammap);
        List<TdFunction> functionList = functionPageInfo.getList();
        Set<String> set = new HashSet();
        for(TdFunction function:functionList){
            set.add(function.getGroupCode());
        }

        Iterator<String> s = set.iterator();
        while(s.hasNext()){
            String groupName = s.next();
            Map<String,Object> map = new HashMap<String, Object>();
            List<TdFunction> list = new ArrayList<TdFunction>();
            for(TdFunction function:functionList){
                if(function.getGroupCode()!=null&&function.getGroupCode().equals(groupName)){
                    list.add(function);
                }
            }
            map.put("groupName",groupName);
            map.put("pemissionList",list);
            listMap.add(map);
        }
        return  setSuccessModelMap(modelMap,listMap);
    }
}
